#!/bin/bash
set -e

while getopts  ":i:p:" opts
do
        case  $opts  in
        i) 
                        intervals=$OPTARG
                        
				;;
		p)
                        out_prefix=$OPTARG
                        
                ;;
		\?)

				echo `basename $0` [-i interval] [-p out_prefix] in.bam
				exit 1
				;;
        esac
done
shift $(($OPTIND - 1))

if test $# -lt 1
then
	echo `basename $0` [-i interval] [-p out_prefix] in.bam
	exit 1
fi

#-----------------------------------------------
#-----------------------------------------------
. /mnt/ilustre/app/medical/tools/.var #---------
#-----------------------------------------------
#-----------------------------------------------

log=.log
if [ ! -e "$log" ]; then
	:> $log
fi


if [ -z $intervals ]; then
	intervals=${data_path}/ref/b37/gw.win200000.bed
	echo interval file is: $intervals 2>>$log 1>&2
else
	echo interval file is: $intervals 2>>$log 1>&2
fi


if [ -z $out_prefix ]; then
	out_prefix=1
fi

echo 2>>$log 1>&2
echo 2>>$log 1>&2
echo gatk CountReads 2>>$log 1>&2
java -jar $gatk \
	-T CountReads \
	-R $ref_genome \
	-I $1 \
	-L ${intervals} \
	> $out_prefix.readcount.txt \
	2>> $log